﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL.VirtualTeam;
using BLL.VirtualTeam.Impl;
using Model;
using BLL.Users;
using BLL.Users.Impl;

public partial class Page_VirtualTeam_VTManager : System.Web.UI.Page
{
    static string RFPId = null;
    
    static Dictionary<string, string> expertNameDictionary = new Dictionary<string, string>();
    static ExpertName expertName = new ExpertName();
    static List<ExpertName> expertNameList = new List<ExpertName>();
    TeamManageBO teamManageBO = new TeamManageBOImpl();
    static Team team = new Team();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
             if (Request["RFPId"] != null)
                {
                    RFPId = Request["RFPId"].ToString();
                    listLeftDatatBind();
                }
                else if (Request["teamId"] != null)
                {
                  
                    TeamDetailBind();
                }
          
        }
    }

    private void TeamDetailBind()
    {
        string teamId = Request["teamId"].ToString();
        //显示团队名和描述
        TeamBO teamBO = new TeamBOImpl();
        team = teamBO.Find(teamId);
        txtTeamName.Text = team.TeamName;
        txtTeamNote.Text = team.TeamDescription;

        //右边的listbox绑定团队成员
        expertNameList = teamManageBO.getExpertNameList(teamId);
        foreach (ExpertName name in expertNameList)
        {
            ListItem item = new ListItem();
            item.Value = name.ExpertId;
            item.Text = name.Name;
            //不显示创建者
            if (name.ExpertId != Session["userId"].ToString())
            {
                ExpertRightList.Items.Add(item);
            }
        }
        RFPId = team.TeamRFPId;
        listLeftDatatBind();
    }

    private void listLeftDatatBind()
    {
        expertNameDictionary.Clear();
        //根据RFP的id获取所有回复实体
        RFPReplyBO rfpReplyBO = new RFPBOReplyImpl();
        List<RFPReply> rfpReplyList = rfpReplyBO.GetEntityList("RFPId", RFPId,"RFPReplyDate");

        //获取回复人姓名
        ExpertNameBO expertNameBO = new ExpertNameBOImpl();
        if (rfpReplyList.Count > 0)
        {
            //遍历回复实体中所有的回复记录
            foreach (RFPReply reply in rfpReplyList)
            {
                //根据回复人编号获取回复人expertName视图对象
                expertName = expertNameBO.Find(reply.RFPReplyExpert);                

                //初始化左边的listbox                   
                ListItem item = new ListItem();
                item.Value = expertName.ExpertId;
                item.Text = expertName.Name;
                ExpertLeftList.Items.Add(item);
               
            }
            //除去左边重复项
            for (int i = 0; i < ExpertLeftList.Items.Count; i++)
            {                
                for (int j = i + 1; j < ExpertLeftList.Items.Count; j++)
                {
                    if (ExpertLeftList.Items[i].Value == ExpertLeftList.Items[j].Value)
                    {
                        ExpertLeftList.Items.Remove(ExpertLeftList.Items[j]);
                    }
                }             
            }
            //除去右边存在的项
            for (int k = 0; k < ExpertRightList.Items.Count; k++)
            {
                for (int i = 0; i < ExpertLeftList.Items.Count; i++)
                {                 
                    if (ExpertLeftList.Items[i].Value == ExpertRightList.Items[k].Value)
                    {
                        ExpertLeftList.Items.Remove(ExpertLeftList.Items[i]);
                    }
                }
            }
        }
    }
    //左边的listbox中的项移动到右边
    protected void btnVTManagerAddTo_Click(object sender, ImageClickEventArgs e)
    {
        for (int i = 0; i < ExpertLeftList.Items.Count; i++)
        {
            if (ExpertLeftList.Items[i].Selected)
            {
                ExpertRightList.Items.Add(ExpertLeftList.Items[i]);
                ExpertLeftList.Items.Remove(ExpertLeftList.Items[i]);
                i--;
            }
        }
    }
    //右边的listbox中的项移动到左边
    protected void btnVTManagerDeleteFrom_Click(object sender, ImageClickEventArgs e)
    {
        for (int i = 0; i < ExpertRightList.Items.Count; i++)
        {
            if (ExpertRightList.Items[i].Selected)
            {
                ExpertLeftList.Items.Add(ExpertRightList.Items[i]);
                ExpertRightList.Items.Remove(ExpertRightList.Items[i]);
                i--;
            }
        }
    }

    //组建或更新团队
    protected void btnVTManagerSubmit_Click(object sender, ImageClickEventArgs e)
    {
        bool result = false;
        //收集数据
        string teamName = txtTeamName.Text.Trim();
        string teamNote = txtTeamNote.Text.Trim();

        //初始化专家姓名列表
        List<string> idList = new List<string>();
        //遍历右边listbox中的值,并添加进专家id列表
        for (int i = 0; i < ExpertRightList.Items.Count; i++)
        {
            idList.Add(ExpertRightList.Items[i].Value);
        }
        //更新团队信息
        if (Request["teamId"] != null)
        {
            string teamId = Request["teamId"].ToString();
            Team updateTeam = new Team()
            {
                TeamName = teamName,
                TeamRFPId = RFPId,
                TeamDescription = teamNote,
                TeamId = teamId,
                TeamDate = team.TeamDate,
                TeamLeader=Session["userId"].ToString()
            };
            result = teamManageBO.UpdateTeam(updateTeam, idList);
        }
        //组建新团队
        else
        {
            Team newTeam = new Team()
            {
                TeamName = teamName,
                TeamRFPId = RFPId,
                TeamDescription = teamNote,
                TeamLeader = Session["userId"].ToString()
            };
            result = teamManageBO.BuildTeam(newTeam, idList);
        }
        if (result)
        {
            Response.Write("<script language=javascript>alert('Team Build success！')</script>");
            Response.Write("<script language=javascript>window.location.href='TeamList.aspx'</script>");
        }
        else
        {
            Response.Write("<script>alert('Fail!')</script>");
        }
    }
    //返回
    protected void btnVTManagerBack_Click(object sender, ImageClickEventArgs e)
    {
        Response.Write("<script language=javascript>history.go(-2);</script>");
    }
}